home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1656 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.1 KB

  1. From: SimsGW@msn.com (Gary Sims)
  2. Subject: RE: How to initialize an array of structures?
  3. Date: 12 Jan 96 03:17:18 -0800
  4. References: <4d240e$nk5@jupiter.planet.net>
  5. Message-ID: <00001a81+00008c58@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c++
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. This is the syntax for assigning a value to one element of an array 
  11. of structures:
  12.  
  13. static struct FUNCTIONMAP functionlist[300];    // declared with 
  14. appropriate scope
  15.  
  16.  
  17. functionlist[0]=FUNCTIONMAP("firstfunction",1,3,2,2,2,0,0);    // First 
  18. function is index zero
  19.  
  20. To do that however, you must declare constructors for the structure. 
  21. You need two of them, declared like this in an appropriate header 
  22. file that you can include in any module that uses the FUNCTIONMAP 
  23. structures:
  24.  
  25. struct FUNCTIONMAP
  26. {
  27.     // attributes
  28.     char     *functionname;
  29.     int    functionnumber;
  30.     int    maxargs;
  31.     int    functarg[5];
  32.     // constructors
  33.     FUNCTIONMAP();    // default constructor
  34.     FUNCTIONMAP(
  35.         const char* Name,
  36.         const int Num, const int Args, 
  37.         const int Fa0, const int Fa1,
  38.         const int Fa2, const int Fa3, const int Fa4
  39.         );        // constructor with arguments
  40. };
  41.  
  42. and coded something like this in a separate implementation file:
  43.  
  44. #include "FUNCTIONMAP.h"
  45. //...
  46. FUNCTIONMAP::FUNCTIONMAP()
  47. {
  48.     functionname=NULL; functionnumber=0; maxargs=0;
  49.     for(int i=0; i<5; i++) functarg[i]=0;
  50. };
  51.  
  52. FUNCTIONMAP::FUNCTIONMAP(const char* Name,const int Num, const int Args, 
  53.             const int Fa0, const int Fa1, const int Fa2, const int Fa3, const int Fa4)
  54. {
  55.     functionname=NULL;
  56.     if(NULL!=Name) functionname=new char[strlen(Name)+1];
  57.     if(NULL!=functionname) strcpy(functionname,Name);
  58.     functionnumber=Num;
  59.     maxargs=Args;
  60.     functarg[0]=Fa0;
  61.     functarg[1]=Fa1;
  62.     functarg[2]=Fa2;
  63.     functarg[3]=Fa3;
  64.     functarg[4]=Fa4;
  65. };
  66.  
  67. I'm sure that the C programming language had some syntax for 
  68. assigning values to the members of a structure, but I don't know what 
  69. it might have been. Since you posted your question in comp.lang.c++ I 
  70. assume you want a C++ answer.
  71.  
  72. Incidentally, it's uncommon to use the struct type in C++ 
  73. programming. It's outdated usage now. A class is the preferred usage. 
  74.  You can just substitute "class" for "struct" to re-compile this as a 
  75. class, but then you must add "public:" right after the opening 
  76. bracket to make a class that behaves like a struct (by exposing its 
  77. members to the world at large). Or you can follow C++ programming 
  78. style and provide public methods that do the things you want this 
  79. class to do while keeping its attributes and implementation private.
  80.  
  81. Changed to a class with a couple of access methods, the declaration 
  82. would like something like this:
  83.  
  84. class FUNCTIONMAP
  85. {
  86.     // attributes
  87.     char     *functionname;
  88.     int    functionnumber;
  89.     int    maxargs;
  90.     int    functarg[5];
  91.   public:
  92.     // constructors
  93.     FUNCTIONMAP();    // default constructor
  94.     FUNCTIONMAP(
  95.         const char* Name,
  96.         const int Num, const int Args, 
  97.         const int Fa0, const int Fa1,
  98.         const int Fa2, const int Fa3, const int Fa4
  99.         );        // constructor with arguments
  100.     // access to data and operations
  101.     const char* FtnName(){return functionname;};
  102.     ResultCode RunThyself();
  103. };
  104.  
  105. Good luck,
  106.  
  107. Gary W. Sims
  108. Stonehaven Laboratory
  109.